package com.esd.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.esd.device.entity.ChannelType;
import com.esd.device.entity.DeviceChannel;
import com.esd.device.mapper.ChannelTypeMapper;
import com.esd.device.mapper.DeviceChannelMapper;
import com.esd.device.service.DeviceChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceChannelSeviceImpl extends ServiceImpl<DeviceChannelMapper , DeviceChannel> implements DeviceChannelService {

    @Autowired
    private DeviceChannelMapper deviceChannelMapper ;

    @Override
    public List<DeviceChannel> getListByDeviceId(Long deviceId) {
        LambdaQueryWrapper<DeviceChannel> query = new LambdaQueryWrapper<>() ;
        query.eq(DeviceChannel::getDeviceId , deviceId) ;
        return baseMapper.selectList(query) ;
    }

    @Override
    public void checkChOn(Long deviceId, int i, boolean chOn) {
        List<DeviceChannel> deviceChannels = getListByDeviceId(deviceId);

        if ( i < deviceChannels.size()){
            DeviceChannel deviceChannel = deviceChannels.get(i);
            deviceChannel.setEnabled(chOn);
            baseMapper.updateById(deviceChannel) ;
        }
    }

    @Autowired
    private ChannelTypeMapper channelTypeMapper ;

    @Override
    public void checkChTypes(Long deviceId, int i, String s) {
        List<DeviceChannel> deviceChannels = getListByDeviceId(deviceId);
        String channelTypeName ;
        switch (s) {
            case "01":
                channelTypeName = "ESD手环";
                break;
            case "02":
                channelTypeName = "台垫接地";
                break;
            case "03":
                channelTypeName = "设备接地";
                break;
            default:
                return;
        }
        LambdaQueryWrapper<ChannelType> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.eq(ChannelType::getChannelTypeName , channelTypeName) ;
        ChannelType channelType = channelTypeMapper.selectOne(queryWrapper, false);
        if (channelType == null){
            return ;
        }
        if ( i < deviceChannels.size()){
            DeviceChannel deviceChannel = deviceChannels.get(i);
            deviceChannel.setChannelTypeId(channelType.getChannelTypeId());
            baseMapper.updateById(deviceChannel) ;
        }
    }
}
